package com.xiao.framework.ds.autoconfigure.aspectj;

import com.xiao.framework.ds.annotation.Database;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

/**
 * @author <a href="mailto:xiaoquanidea@163.com">hutiquan</a>
 * @since 2019-08-24 13:12
 */
public class MultiDataSourcePointcutAdvisor extends AbstractPointcutAdvisor {

  private final Advice advice;

  public MultiDataSourcePointcutAdvisor(Advice advice) {
    this.advice = advice;
  }


  @Override
  public Pointcut getPointcut() {
    return buildPointcut();
  }

  private Pointcut buildPointcut() {
    AnnotationMatchingPointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(Database.class);
    AnnotationMatchingPointcut cpc = new AnnotationMatchingPointcut(Database.class, true);

    //直接new 默认匹配所有方法
    ComposablePointcut composablePointcut = new ComposablePointcut();
    //相交
    composablePointcut.intersection(mpc)
        //并集
        .union(cpc);
    return composablePointcut;
  }

  @Override
  public Advice getAdvice() {
    return advice;
  }
}
